﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Common;
using LY.DataAccess.Repository;
using LY.Entity;
using LY.Interface;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;

namespace LY.Business.ServiceImpl
{
     public class UserService:IUserService
    {
         UserRepository _urepo;
         public UserService()
         {
             _urepo = new UserRepository();
         }
        public IUser GetUserInfo(string userId)
        {           
            return new IdentityUser()
            {
                UserId = "TEST_USER",
                FullName = "测试用户",
                EmployID = "1001001",
                StoreId = 1001,
                OrgId =1 
            };
        }

        public List<IPrivilege> GetUserPrivilegeList(string userId)
        {
            return null;
        }

        public List<S_User> GetUpperUserList(int? id)
        {
            return _urepo.GetUpperUserList(id);
        }

        public List<S_User> GetUserList(int deptid)
        {
            return _urepo.GetUserList(deptid);
        }

        /// <summary>
        /// 新用户有权限的模块列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<S_User_EX> QueryRoleUserList()
        {
            return _urepo.QueryRoleUserList();
        }

        /// <summary>
        /// 用户有权限的模块列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<S_User_EX> QueryRoleUserList(int roleid)
        {
            return _urepo.QueryRoleUserList(roleid);
        }


        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<S_User> QueryUserList(UserSearch search, PageView view)
        {
            return _urepo.QueryUserList(search, view);
        }

        //用户实体
        public S_User GetUser(int? id)
        {
            return _urepo.GetUser(id);
        }

        //保存用户
        public int SaveUser(S_User user)
        {

            if (user.ID <= 0)
            {
                //新增
                int ret = _urepo.CheckUser(user.LoginName);
                if (ret > 0)
                {
                    return -2;//已存在相同的用户名
                }
                user.Password = LY.Common.Utils.MD5Util.GetMD5_32("123456");
                return (int)_urepo.Insert(user);
            }
            else
            {
                int ret = _urepo.CheckUser(user.LoginName);
                if (ret >= 2 || ret == 1)
                {

                }
                user.RemoveUpdateColumn("LoginName");
                user.RemoveUpdateColumn("Password");
                //编辑
                return (int)_urepo.Update(user);
            }
        }

        //删除用户
        public int DeleteUser(int? id)
        {
            return _urepo.DeleteUser(id);
        }

        //根据当前用户的Id 查询出他的领导
        public S_User SelectUpUser(int id)
        {
            return _urepo.SelectUpUser(id);
        }

         /// <summary>
         /// 修改密码
         /// </summary>
         /// <param name="pwd"></param>
         /// <param name="id"></param>
         /// <returns></returns>
        public int UpdatePwd(string pwd, int id)
        {
            return _urepo.UpdatePwd(pwd,id);
        }
    }
}
